package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.RedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2018/4/26.
 */
@Controller
@RequestMapping(ControllerConstants.REDPACKET)
public class RedPacketMCtronller extends BaseMobileController {
    @Autowired
    private RedPacketService redPacketService;

    /**
     * 发布红包
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addRedPacket")
    public void addRedPacket(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer busId = getInt(json, "busId", 0); //商家id
                Integer number = getInt(json, "number", 0); //红包个数
                Integer payType = getInt(json, "payType", 0); //红包个数
                Integer cityId = getInt(json, "cityId", 0); //城市编号
                String content = getString(json, "content", "");
                double money = getDouble(json, "money", 0);//金额
                return redPacketService.addRedPacket(busId, number, money, content, payType, cityId);
            }
        });
    }

    /**
     * 修改红包
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateRedPacketStatus")
    public void updateRedPacketStatus(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer redPacketId = getInt(json, "redPacketId", 0); //商家id

                return redPacketService.updateRedPacketStatus(redPacketId);
            }
        });
    }

    /**
     * 查询所有的红包信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findRedPacketList")
    public void findRedPacketList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                Integer cityId = getInt(json, "cityId", 0); //城市编号
                Integer userId = getInt(json, "userId", 0);
                return redPacketService.findRedPacketList(nowPage, pageSize, userId, cityId);
            }
        });
    }

    /**
     * 抢红包
     *
     * @param request
     * @param response
     */
    @RequestMapping("/grabRedPacket")
    public void grabRedPacket(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer redPacketId = getInt(json, "redPacketId", 0);
                String trendsUserId = getString(json, "trendsUserId", "0");
                return redPacketService.grabRedPacket(userId, redPacketId, trendsUserId);
            }
        });
    }

    /**
     * 查看用户抢红包的记录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findHistoryByUserId")
    public void findHistoryByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return redPacketService.findHistoryByUserId(userId, nowPage, pageSize);
            }
        });
    }

    /**
     * 查看用户红包信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getRedPacketByUserId")
    public void getRedPacketByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                return redPacketService.getRedPacketByUserId(userId);
            }
        });
    }

    /**
     * 查看用户红包信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findRedPacketHistory")
    public void findRedPacketHistory(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer companyId = getInt(json, "companyId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return redPacketService.findRedPacketHistory(companyId, nowPage, pageSize);
            }
        });
    }

    /**
     * 查看红包信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getRedPacketById")
    public void getRedPacketById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer redPacketId = getInt(json, "redPacketId", 0);
                Integer userId = getInt(json, "userId", 0);
                return redPacketService.getRedPacketById(redPacketId, userId);
            }
        });
    }

}
